﻿using System.Collections.Generic;
using WeiboPlus.Common;
using WeiboPlus.Models;
using WeiboPlus.Service;
#if SINA
using WeiboPlus.Sina;
#endif
#if RENREN
using WeiboPlus.Renren;
#endif
#if TENCENT
using WeiboPlus.Tencent;
#endif
//using WeiboPlus.Twitter;

namespace WeiboPlus
{
    public class SNSProviderManager
    {
        private static readonly SNSProviderManager _current = new SNSProviderManager();
        public static SNSProviderManager Current
        {
            get { return _current; }
        }

        Dictionary<SNSProviderType, SNSProvider> _dic = new Dictionary<SNSProviderType, SNSProvider>();

        private SNSProviderManager()
        {
            Load();
        }


        private void Load()
        {

        }

        public SNSProvider GetProvider(SNSProviderType pType)
        {
            if (!_dic.ContainsKey(pType))
            {
                lock (_dic)
                {
                    var factory = GetFactory(pType);
                    if (factory == null)
                    {
                        return null;
                    }
                    var provider = factory.GetProvider();

                    if (provider != null)
                    {
                        _dic.Add(pType, provider);
                    }
                }
            }
            return _dic[pType];
        }

        public static SNSProviderFactory GetFactory(SNSAccount account)
        {
            var type = account.ServiceProvider;
            var rv = GetFactory(type);

            return rv;
        }

        public static SNSProviderFactory GetFactory(SNSProviderType type)
        {
            switch (type)
            {
#if SINA
                case SNSProviderType.Sina:
                    return SinaSNSProviderFactory.Instance;
#endif
#if TENCENT
                case SNSProviderType.Tencent:
                    return TencentSNSProviderFactory.Instance;
#endif
#if RENREN
                case SNSProviderType.RenRen:
                    return RenRenSNSProviderFactory.Instance;
#endif
                //case SNSProviderType.Twitter:
                //    return TwitterSNSProviderFactory.Instance;
                case SNSProviderType.Douban:
                case SNSProviderType.Sohu:
                default:
                    return null;
            }
        }

        public static IMappingFactory CreateFactory(SNSAccount account)
        {
            switch (account.ServiceProvider)
            {
#if SINA
                case SNSProviderType.Sina:
                    return new SinaMappingFactory(account);
#endif
#if TENCENT
                case SNSProviderType.Tencent:
                    return new TencentMappingFactory(account);
#endif
#if RENREN
                case SNSProviderType.RenRen:
                    return new RenrenMappingFactory(account);
#endif
                default:
                    return null;
            }
        }

        public IEnumerable<SNSProvider> GetAllSupportedProviders()
        {
            List<SNSProvider> all = new List<SNSProvider>();
            foreach (var p in EnumHelper.GetValues<SNSProviderType>())
            {
                var provider = GetProvider(p);
                if (provider != null && provider.IsSupported)
                    all.Add(provider);
            }
            return all;
        }
    }
}
